<template>
  <div class="app-container">
    <div
      class="header"
      style="display: flex; justify-content: space-between"
    >
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item>
          <el-button
            icon="refresh"
            color=" rgb(69, 76, 105)"
            style="color: #fff"
            @click="resetQuery()"
          />
        </el-form-item>
      </el-form>
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item
          label=""
          prop="couponName"
        >
          <el-input
            v-model="queryParams.couponName"
            placeholder="请输入优惠券名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item
          label=""
          prop="type"
        >
          <el-select
            v-model="queryParams.type"
            placeholder="请选择优惠券类型"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          >
            <el-option
              v-for="(item, index) in allType"
              :key="index"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label=""
          prop="state"
        >
          <el-select
            v-model="queryParams.state"
            placeholder="请选择状态"
            clearable
            style="width: 150px"
            @keyup.enter="handleQuery"
          >
            <el-option
              v-for="(item, index) in allState"
              :key="index"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="info"
            icon="Search"
            @click="handleQuery()"
          />
        </el-form-item>
      </el-form>
    </div>

    <el-table
      v-loading="loading"
      :data="couponList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="优惠券名称"
        align="center"
        prop="couponName"
      />
      <el-table-column
        label="优惠券类型"
        align="center"
        prop="type"
      >
        <!-- <template #default="scope">
          {{ allType.find((item) => item.value == scope.row.type).label }}
        </template> -->
        <template #default="scope">
          <!-- {{ allType.find((item) => item.value == scope.row.type).label }} -->
            {{ scope.row.type==0?'通用打折券':scope.row.type==1?'顺风车打折券': scope.row.type==2?'成际打折券':'' }}
        </template>
      </el-table-column>
      <el-table-column
        label="领取日期"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="发布数量"
        align="center"
        prop="publishNum"
      >
        <template #default="scope">
          {{ scope.row.publishNum == -1 ? "不限量" : scope.row.publishNum }}
        </template>
      </el-table-column>
      <!-- 问题所在处 ------后代返回的state为空导致 -->
      <el-table-column
        label="状态"
        align="center"
        prop="state"
      >
        <template #default="scope">
          <!-- {{ allState.find((item) => item.value == scope.row.state).label }} -->
            {{ scope.row.state==0?'开启':scope.row.state==1?'关闭':'' }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:coupon:edit']"
            >编辑</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:coupon:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠券对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      width="500px"
      append-to-body
    >
      <el-form
        ref="couponRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item
          label="状态"
          prop="state"
        >
          <el-radio-group v-model="form.state">
            <el-radio label="0">开启</el-radio>
            <el-radio label="1">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button
            type="primary"
            @click="submitForm"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Publish">
import {
  listPublish,
  delPublish,
  addPublish,
  updatePublish,
} from "@/api/system/publish";

const { proxy } = getCurrentInstance();

const couponList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  allState: [
    {
      label: "关闭",
      value: 1,
    },
    {
      label: "开启",
      value: 0,
    },
  ],
  allType: [
    {
      label: "通用打折券",
      value: 0,
    },
    {
      label: "顺风车打折券",
      value: 1,
    },
    {
      label: "成际打折券",
      value: 2,
    },
  ],
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    couponName: null,
    type: null,
    state: null,
  },
  rules: {
    state: [{ required: true, message: "状态不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules, allState, allType } = toRefs(data);

/** 查询优惠券列表 */
function getList() {
  loading.value = true;
  console.log(queryParams.value,'queryParams.value11111111111111111100')
  listPublish(queryParams.value).then((response) => {
    console.log(response,'respose获取到的发布优惠券的列表')
    couponList.value = response.rows;
    console.log(couponList.value,'发布优惠券的列表赋值给data成功吗')
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    publishId: null,
    couponName: null,
    type: null,
    couponAmount: null,
    lowestConsume: null,
    availableDate: null,
    state: null,
    createTime: null,
    updateTime: null,
  };
  proxy.resetForm("couponRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  console.log(queryParams.value,"查询参数,点击搜索按钮时传递的参数")
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.publishId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加优惠券";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value = row;
  open.value = true;
  title.value = "修改优惠券";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["couponRef"].validate((valid) => {
    if (valid) {
      if (form.value.publishId != null) {
        updatePublish(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPublish(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _publishIds = row.publishId || ids.value;
  proxy.$modal
    .confirm('是否确认删除优惠券编号为"' + _publishIds + '"的数据项？')
    .then(function () {
      return delPublish(_publishIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
